import PyPDF2
import math

def split_pdf(input_path, output_prefix, num_splits):
    # 打开输入的PDF文件
    with open(input_path, 'rb') as file:
        reader = PyPDF2.PdfReader(file)
        num_pages = len(reader.pages)
        # 计算每个输出文件应包含的页数
        pages_per_split = math.ceil(num_pages / num_splits)

        for i in range(num_splits):
            writer = PyPDF2.PdfWriter()
            start_page = i * pages_per_split
            end_page = min((i + 1) * pages_per_split, num_pages)

            # 将页面添加到输出文件中
            for page_num in range(start_page, end_page):
                writer.add_page(reader.pages[page_num])

            # 保存输出文件
            output_path = f"{output_prefix}_{i + 1}.pdf"
            with open(output_path, 'wb') as output_file:
                writer.write(output_file)

if __name__ == "__main__":
    input_pdf = r"C:\Users\Administrator\Documents\WeChat Files\guofeifei\FileStorage\File\2025-05\信息系统项目管理师教程-第四版.pdf"
    output_prefix = "output_pdf"
    num_splits = 2  # 可根据需要修改拆分数量
    split_pdf(input_pdf, output_prefix, num_splits)
